//
//  ReadyPayViewController.m
//  cityStar
//
//  Created by Hancong on 14/12/26.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "ReadyPayViewController.h"
#import "CommodityReadyPayTableViewCell.h"
#import "PayCommodityViewController.h"
#import "SelectDistributionViewController.h"
#import "AddDeliveryViewController.h"
#import "IntegralViewController.h"
#import "RemarksViewController.h"
#import "DeliveryAddressViewController.h"
#import "UIActionSheet+Blocks.h"
#import "RIButtonItem.h"
#import "BuycartGood.h"

#import "WaresDetailViewController.h"

NSString *const commodityReadyPayTableViewCellID = @"CommodityReadyPayTableViewCellID";

@interface ReadyPayViewController ()<selectDistributionVCDelegate,remarksVCDelegate>
@property (nonatomic,weak) IBOutlet UIButton *btnSubmit;
@property (nonatomic,weak) IBOutlet UITableView *tableView;
@property (nonatomic,weak) IBOutlet UIView *headerView;
@property (nonatomic,weak) IBOutlet UIButton *btndispatch;
//@property (nonatomic,weak) IBOutlet UIView *footerView;
@property (nonatomic,strong) NSArray *requestArray;
@property (nonatomic,strong) NSString *money;
@property (nonatomic,strong) NSString *addressUUID;

@end

@implementation ReadyPayViewController

-(instancetype)initWithCommodityArray:(NSArray *)commodityArr andMoney:(NSString *)money
{
    self = [super init];
    if (self) {
        self.requestArray = [[NSArray alloc]initWithArray:commodityArr];
        self.money = money;
    }
    return self;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"确认订单";
    [self addBackButton];
    [self setUpTableViews];
    self.labelMoney.text = self.money;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(senderdidClick:) name:@"senderdid" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(senderUUIDlick:) name:@"senderUUID" object:nil];
    
    self.btnSubmit.clipsToBounds = YES;
    self.btnSubmit.layer.cornerRadius = 4;
    self.parameters = [[NSMutableDictionary alloc]init];
    [self webRequest_getDefaultsAddress];
}

-(void)senderdidClick:(NSNotification *)notification{
    NSString *str = (NSString *)notification.object;
    self.labelAddress.text = str;
}

//获取UUid
-(void)senderUUIDlick:(NSNotification *)notification
{
    NSString *string  = (NSString *)notification.object;
    [self.parameters removeObjectForKey:@"address"];
    [self.parameters setValue:string forKey:@"address"];
}

-(void)setUpTableViews
{
    self.tableView.tableHeaderView = self.headerView;
//    self.tableView.tableFooterView = self.footerView;
    [self.tableView registerNib:[CommodityReadyPayTableViewCell nib] forCellReuseIdentifier:commodityReadyPayTableViewCellID];
}

//-(void)viewWillAppear:(BOOL)animated
//{
//    [self webRequest_getDefaultsAddress];
//}

#pragma mark - webRequest
-(void)webRequest_getDefaultsAddress
{
    [HttpRequestManager getMethod:KInterface_getDefaultsAddress parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            self.labelAddress.text = response[@"value"][@"address"];
            self.addressUUID = response[@"value"][@"uuid"];
            [self.parameters removeObjectForKey:@"address"];
            [self.parameters setValue:self.addressUUID forKey:@"address"];
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];
}

#pragma mark - UITableViewDelegate and UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommodityReadyPayTableViewCell *commodityReadyPayCell = [tableView dequeueReusableCellWithIdentifier:commodityReadyPayTableViewCellID];
    commodityReadyPayCell.selectionStyle = UITableViewCellSelectionStyleGray;
    BuycartGood *goodcart = self.requestArray[indexPath.row];
    [commodityReadyPayCell configWithGoodCart:goodcart];
        //    [self.groupBuyTableViewCell configWithMessage:message];
    return commodityReadyPayCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [CommodityReadyPayTableViewCell heightOfTableCell];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.requestArray.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BuycartGood *goodCart = self.requestArray[indexPath.row];
    WaresDetailViewController *waresDetailViewController = [[WaresDetailViewController alloc]initWithCommodityID:goodCart.goods_id];
    [self.navigationController pushViewController:waresDetailViewController animated:YES];
    [tableView reloadData];
}

#pragma mark - Action
-(IBAction)readyPay_Touch:(id)sender
{
    if (![self.parameters objectForKey:@"deliver"]) {
        BossSystemShowAlertViewWithTitle(@"请选择配送方式");
        return;
    }else if (![self.parameters objectForKey:@"range"]){
        BossSystemShowAlertViewWithTitle(@"请选择配送时间");
        return;
    }
    PayCommodityViewController *payCommodityVC = [[PayCommodityViewController alloc]initWithCommodityArray:self.requestArray andAddress:self.labelAddress.text];
    payCommodityVC.parameters = [[NSMutableDictionary alloc]initWithDictionary:self.parameters];
    [self.navigationController pushViewController:payCommodityVC animated:YES];
    DLog(@"确认订单");
}

- (IBAction)selectAddress:(id)sender {
    DeliveryAddressViewController *deliveryVC = [[DeliveryAddressViewController alloc]init];
//    deliveryVC.delegate = self;
    [self.navigationController pushViewController:deliveryVC animated:YES];

}
- (IBAction)selectTopeisong:(id)sender {
    SelectDistributionViewController *selectAddressVC = [[SelectDistributionViewController alloc]init];
    selectAddressVC.delegate = self;
    [self.navigationController pushViewController:selectAddressVC animated:YES];
}
- (IBAction)selectTobeizhu:(id)sender {
    RemarksViewController *remarkVC = [[RemarksViewController alloc]init];
    remarkVC.delegate = self;
    [self.navigationController pushViewController:remarkVC animated:YES];

}
- (IBAction)selectTimes:(id)sender {
    RIButtonItem *btnFirst = [RIButtonItem itemWithLabel:@"均可" action:^{
        self.labelTime.text = @"均可";
        [self.parameters removeObjectForKey:@"range"];
        [self.parameters setValue:@"0" forKey:@"range"];
    }];
    RIButtonItem *btnSecond = [RIButtonItem itemWithLabel:@"上午（12点前）" action:^{
        self.labelTime.text = @"上午（12点前）";
        [self.parameters removeObjectForKey:@"range"];
        [self.parameters setValue:@"1" forKey:@"range"];
    }];
    RIButtonItem *btnThird = [RIButtonItem itemWithLabel:@"下午（17点前）" action:^{
        self.labelTime.text = @"下午（17点前）";
        [self.parameters removeObjectForKey:@"range"];
        [self.parameters setValue:@"2" forKey:@"range"];
    }];
    RIButtonItem *btnForth = [RIButtonItem itemWithLabel:@"取消" action:^{
    }];
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"请选择配送时间" cancelButtonItem:btnForth destructiveButtonItem:btnFirst otherButtonItems:btnSecond,btnThird, nil];
    [sheet showInView:self.view];
}

#pragma mark - selectDistributionVCDelegate
-(void)selectDistributionVCChoosePeisongType:(NSString *)type
{
    if ([type intValue] == 1) {
        self.labelpeisong.text = @"快递送货上门";
        [self.parameters removeObjectForKey:@"deliver"];
        [self.parameters setValue:@"2" forKey:@"deliver"];
    }else{
        self.labelpeisong.text = @"线下自提";
        [self.parameters removeObjectForKey:@"deliver"];
        [self.parameters setValue:@"1" forKey:@"deliver"];
    }
}
#pragma mark - remarksDelegate
-(void)remarksVCTouchFinishWithMarks:(NSString *)remarks
{
    self.labelTips.text = remarks;
    [self.parameters removeObjectForKey:@"remark"];
    [self.parameters setValue:remarks forKey:@"remark"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
